using System.Collections.Generic;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace CustomControls.UserControls
{
    public partial class UserControlKeyBinding : UserControlBase
    {
        private IApplicationSettings _localSettings;

        public UserControlKeyBinding()
        {
            InitializeComponent();
            _localSettings = Locator.Resolve<IApplicationSettings>();
        }

        public override void Initialize()
        {
            //_configSettings = new ApplicationSettings();

            radioButtonRefresh.Focus();


            //Set the labels
            labelCopy.Text = _localSettings.KeyList[KeyEnum.COPY].ToString();
            labelDelete.Text = _localSettings.KeyList[KeyEnum.DELETE].ToString();
            labelMakeDir.Text = _localSettings.KeyList[KeyEnum.MAKEDIR].ToString();
            labelMove.Text = _localSettings.KeyList[KeyEnum.MOVE].ToString();
            labelRefresh.Text = _localSettings.KeyList[KeyEnum.REFRESH].ToString();
            labelRename.Text = _localSettings.KeyList[KeyEnum.RENAME].ToString();
            labelTouch.Text = _localSettings.KeyList[KeyEnum.TOUCH].ToString();

            DirtyFlag = false;

        }

        private void radioButtonCopy_KeyDown(object argSender, KeyEventArgs argEventArgs)
        {

            if (argEventArgs.Alt || argEventArgs.Control || argEventArgs.Shift)
                return;

            if (_localSettings.KeyList[KeyEnum.COPY] == argEventArgs.KeyData)
            {
                return;
            }

            if (ReassignKey(argEventArgs))
            {
                _localSettings.KeyList[KeyEnum.COPY] = argEventArgs.KeyData;
                buttonCopyClear.Enabled = true;
            }
            else
            {
                return;
            }

            SetLableText();

            DirtyFlag = true;
        }



        private void radioButtonDelete_KeyDown(object argSender, KeyEventArgs argEventArgs)
        {

            if (argEventArgs.Alt || argEventArgs.Control || argEventArgs.Shift)
                return;

            if (_localSettings.KeyList[KeyEnum.DELETE] == argEventArgs.KeyData)
            {
                return;
            }

            if (ReassignKey(argEventArgs))
            {
                _localSettings.KeyList[KeyEnum.DELETE] = argEventArgs.KeyData;
                buttonDeleteClear.Enabled = true;
            }
            else
            {
                return;
            }

            SetLableText();

            DirtyFlag = true;
        }

        private void radioButtonMakeDir_KeyDown(object argSender, KeyEventArgs argEventArgs)
        {

            if (argEventArgs.Alt || argEventArgs.Control || argEventArgs.Shift)
                return;

            if (_localSettings.KeyList[KeyEnum.MAKEDIR] == argEventArgs.KeyData)
            {
                return;
            }

            if (ReassignKey(argEventArgs))
            {
                _localSettings.KeyList[KeyEnum.MAKEDIR] = argEventArgs.KeyData;
                buttonMakeDirClear.Enabled = true;
            }
            else
            {
                return;
            }

            SetLableText();

            DirtyFlag = true;
        }


        private void radioButtonMove_KeyDown(object argSender, KeyEventArgs argEventArgs)
        {

            if (argEventArgs.Alt || argEventArgs.Control || argEventArgs.Shift)
                return;

            if (_localSettings.KeyList[KeyEnum.MOVE] == argEventArgs.KeyData)
            {
                return;
            }

            if (ReassignKey(argEventArgs))
            {
                _localSettings.KeyList[KeyEnum.MOVE] = argEventArgs.KeyData;
                buttonMoveClear.Enabled = true;
            }
            else
            {
                return;
            }

            SetLableText();

            DirtyFlag = true;
        }



        private void radioButtonRefresh_KeyDown(object argSender, KeyEventArgs argEventArgs)
        {

            if (argEventArgs.Alt || argEventArgs.Control || argEventArgs.Shift)
                return;

            if (_localSettings.KeyList[KeyEnum.REFRESH] == argEventArgs.KeyData)
            {
                return;
            }

            if (ReassignKey(argEventArgs))
            {
                _localSettings.KeyList[KeyEnum.REFRESH] = argEventArgs.KeyData;
                buttonRefreshClear.Enabled = true;
            }
            else
            {
                return;
            }

            SetLableText();

            DirtyFlag = true;
        }


        private void radioButtonRename_KeyDown(object argSender, KeyEventArgs argEventArgs)
        {

            if (argEventArgs.Alt || argEventArgs.Control || argEventArgs.Shift)
                return;

            if (_localSettings.KeyList[KeyEnum.RENAME] == argEventArgs.KeyData)
            {
                return;
            }

            if (ReassignKey(argEventArgs))
            {
                _localSettings.KeyList[KeyEnum.RENAME] = argEventArgs.KeyData;
                buttonRenameClear.Enabled = true;
            }
            else
            {
                return;
            }

            SetLableText();

            DirtyFlag = true;
        }


        private void radioButtonTouch_KeyDown(object argSender, KeyEventArgs argEventArgs)
        {

            if (argEventArgs.Alt || argEventArgs.Control || argEventArgs.Shift)
                return;

            if (_localSettings.KeyList[KeyEnum.TOUCH] == argEventArgs.KeyData)
            {
                return;
            }

            if (ReassignKey(argEventArgs))
            {
                _localSettings.KeyList[KeyEnum.TOUCH] = argEventArgs.KeyData;
                buttonTouchClear.Enabled = true;
            }
            else
            {
                return;
            }

            SetLableText();

            DirtyFlag = true;
        }

        public override void OnApply() //object sender, EventArgs argPaintArgs)
        {
            if (!DirtyFlag)
            {
                return;
            }

            _localSettings.Save();
            DirtyFlag = false;

        }

        private void buttonCopyClear_Click(object argSender, System.EventArgs argEventArgs)
        {
            _localSettings.KeyList[KeyEnum.COPY] = Keys.None;
            buttonCopyClear.Enabled = false;
            labelCopy.Text = @"None";
            DirtyFlag = true;
        }

        private void buttonDeleteClear_Click(object argSender, System.EventArgs argEventArgs)
        {
            _localSettings.KeyList[KeyEnum.DELETE] = Keys.None;
            buttonDeleteClear.Enabled = false;
            labelDelete.Text = @"None";
            DirtyFlag = true;
        }

        private void buttonMakeDirClear_Click(object argSender, System.EventArgs argEventArgs)
        {
            _localSettings.KeyList[KeyEnum.MAKEDIR] = Keys.None;
            buttonMakeDirClear.Enabled = false;
            labelMakeDir.Text = @"None";
            DirtyFlag = true;
        }

        private void buttonMoveClear_Click(object argSender, System.EventArgs argEventArgs)
        {
            _localSettings.KeyList[KeyEnum.MOVE] = Keys.None;
            buttonMoveClear.Enabled = false;
            labelMove.Text = @"None";
            DirtyFlag = true;
        }

        private void buttonRefreshClear_Click(object argSender, System.EventArgs argEventArgs)
        {
            _localSettings.KeyList[KeyEnum.REFRESH] = Keys.None;
            buttonRefreshClear.Enabled = false;
            labelRefresh.Text = @"None";
            DirtyFlag = true;
        }


        private void buttonRenameClear_Click(object argSender, System.EventArgs argEventArgs)
        {
            _localSettings.KeyList[KeyEnum.RENAME] = Keys.None;
            buttonRenameClear.Enabled = false;
            labelRename.Text = @"None";
            DirtyFlag = true;
        }


        private void buttonTouchClear_Click(object argSender, System.EventArgs argEventArgs)
        {
            _localSettings.KeyList[KeyEnum.TOUCH] = Keys.None;
            buttonTouchClear.Enabled = false;
            labelTouch.Text = @"None";
            DirtyFlag = true;
        }

        private void SetLableText()
        {
            labelCopy.Text = _localSettings.KeyList[KeyEnum.COPY].ToString();
            labelDelete.Text = _localSettings.KeyList[KeyEnum.DELETE].ToString();
            labelMakeDir.Text = _localSettings.KeyList[KeyEnum.MAKEDIR].ToString();
            labelMove.Text = _localSettings.KeyList[KeyEnum.MOVE].ToString();
            labelRefresh.Text = _localSettings.KeyList[KeyEnum.REFRESH].ToString();
            labelRename.Text = _localSettings.KeyList[KeyEnum.RENAME].ToString();
            labelTouch.Text = _localSettings.KeyList[KeyEnum.TOUCH].ToString();
        }

        private bool ReassignKey(KeyEventArgs argEventArgs)
        {
            KeyEnum usedKey = KeyEnum.COPY;

            if (_localSettings.KeyList.ContainsValue(argEventArgs.KeyData))
            {
                //Loop through and find key
                foreach (KeyValuePair<KeyEnum, Keys> currentPair in _localSettings.KeyList)
                {
                    if (currentPair.Value == argEventArgs.KeyData)
                    {
                        usedKey = currentPair.Key;
                        break;
                    }
                }

                string warningMessage = argEventArgs.KeyData.ToString() + " is already assigned to '" + usedKey.ToString() + "'. Do you wish to clear the previous assignment?";
                DialogResult response = MessageBox.Show(warningMessage, @"Key assigned", MessageBoxButtons.YesNo);
                if (response == DialogResult.No)
                {
                    return false;
                }

                _localSettings.KeyList[usedKey] = Keys.None;

                switch (usedKey)
                {
                    case KeyEnum.COPY:
                        buttonCopyClear.Enabled = false;
                        break;
                    case KeyEnum.DELETE:
                        buttonDeleteClear.Enabled = false;
                        break;
                    case KeyEnum.MAKEDIR:
                        buttonMakeDirClear.Enabled = false;
                        break;
                    case KeyEnum.MOVE:
                        buttonMoveClear.Enabled = false;
                        break;
                    case KeyEnum.REFRESH:
                        buttonRefreshClear.Enabled = false;
                        break;
                    case KeyEnum.RENAME:
                        buttonRenameClear.Enabled = false;
                        break;
                    case KeyEnum.TOUCH:
                        buttonTouchClear.Enabled = false;
                        break;
                }
            }
            return true;
        }

    }
}
